<template>
	<!-- 提现记录 -->
	<view class="">
		<view class="d-flex j-sb  my-4 px-3 font-33">
			<view class="">
				总提现
			</view>
			<view class="theme-color ">
				{{numALL}}
			</view>
		</view>

		<view class=" d-flex a-center j-sb px-2 " style="height: 100rpx; background: #f7f7fc;">
			<picker mode="date" :value="date" :start="startDate" fields='month' :end="endDate" @change="bindDateChange">
				<view class="font-30" style="color: #000; padding: 15rpx 30rpx;background: #fff;border-radius: 50rpx;">
					{{date}}
				</view>


			</picker>
			<!-- 	<view class="theme-color font-33">
				已结算：{{numALL}}
			</view> -->
		</view>
		<view class="pd20 tab-type-list">
			<imgTypeTab :imgTypeList="statusList" ref="imgTypeTab" @tabSelect="tabSelect"></imgTypeTab>
		</view>

		<view class="earnings-list ">
			<view class="earnings-item d-flex j-sb mx-3 a-center height-130 border-b" v-for="(item,index) in list"
				:key="index">
				<view class="left font-33">
					<view class="theme-color">
						提现收益
					</view>
					<view class="theme-color-hui font-27 mt-1">
						{{item.add_data}}
					</view>
				</view>
				<view class="right theme-color font-33">
					-{{Number(item.totalPrice).toFixed(2)}}
				</view>
			</view>


		</view>

		<view class="" style="margin-top: 10rpx;">
			<dataNo v-if="list.length == 0"></dataNo>
		</view>

		<view class="" style="text-align: center;margin-top: 30rpx;" v-if="pageIndex != 1">
			{{none}}
		</view>
	</view>
</template>

<script>
	import imgTypeTab from "@/components/common/img-type-tab.vue"
	export default {
		components: {
			imgTypeTab
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				status: 2,
				statusList: [

					{
						name: "审核中",
						status: 2
					},
					{
						name: "已提现",
						status: 1
					},
					{
						name: "已拒绝",
						status: 3
					}
				],
				user: uni.getStorageSync('user') ? uni.getStorageSync('user') : {},
				list: [],
				none: '',
				pageIndex: 1,
				date: currentDate,
				endDate1: "",
				startDate1: "",
				numALL: 0
			}
		},
		onLoad() {
			this.getList()
			this.getTotal()
		},
		onReachBottom() {
			// this.adUnitIdbanner = this.advertisingData1(this.advertisingDataType.bannerList)
			if (this.none) {
				return
			}
			++this.pageIndex
			this.getList()
		},
		methods: {
			tabSelect(item) {
				this.status = item.status
				this.pageIndex = 1;
				this.none = ''
				this.list = []
				this.getList()
			},
			dateFormat(fmt, date) {
				let ret
				const opt = {
					"Y+": date.getFullYear().toString(), // 年
					"m+": (date.getMonth() + 1).toString(), // 月
					"d+": date.getDate().toString(), // 日
					"H+": date.getHours().toString(), // 时
					"M+": date.getMinutes().toString(), // 分
					"S+": date.getSeconds().toString(), // 秒
				}
				for (let k in opt) {
					ret = new RegExp("(" + k + ")").exec(fmt)
					if (ret) {
						fmt = fmt.replace(
							ret[1],
							ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
						)
					}
				}
				return fmt
			},
			getNowMonthLast(date1) {
				const date = new Date(date1)
				const enddate = new Date(date.getFullYear(), date.getMonth() + 1, 0)
				return enddate
			},
			async getTotal() {
				var data = await this.$api.getSum({
					dbName: "wx_withdraw",
					fieldName:"money",
					whereJson: {
						openid: this.user.openid,
					}
				})
				this.numALL = data.sum
			
			},
			async getList() {
				var arr = this.date.split('-')
				var data = await this.$api.queryTime({
					dbName: "wx_withdraw",
					startTime: new Date(arr[0], arr[1] - 1, 1).getTime(),
					endTime: new Date(arr[0], arr[1], 0).getTime() + 86400000,
					whereJson:{
						openid: this.user.openid,
						status: this.status,
					},
					numField: "$money",
					pageIndex: this.pageIndex,
					pageSize: 50
				}, true)
				
				if (data.rows.length != 0) {
					this.list = data.rows
				} else {
					this.none = '没有数据了'
				}

			},
			bindDateChange: function(e) {
				this.date = e.detail.value
				this.endDate1 = this.getNowMonthLast(this.date).getTime()
				this.startDate1 = new Date(this.date).getTime()
				this.pageIndex = 1;
				this.none = ''
				this.list = []
				this.getList()
			},

			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}`;
			},

		}
	}
</script>

<style>
	page {
		background: #ffffff;
	}
</style>